package com.irun.sm.ui.demo.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/***
 * @author huangsm
 * @date 2013-1-22
 * @email huangsanm@gmail.com
 * @desc 多点触摸view
 */
public class PointView extends SurfaceView implements SurfaceHolder.Callback {

	private final static int MAX_TOUCHPOINTS = 10;
	private final static String START_TEXT = "请随意点击屏幕";

	private int[] mColors = new int[MAX_TOUCHPOINTS];
	private Paint[] mPaints = new Paint[MAX_TOUCHPOINTS];
	private int width, height;
	private float scale = 1.0f;

	private Paint mPaint;

	private SurfaceHolder mHolder;
	
	public PointView(Context context) {
		super(context);
		init();
	}

	public PointView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public PointView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	private void init() {
		mHolder = getHolder();
		mHolder.addCallback(this);
		setFocusable(true);
		setFocusableInTouchMode(true);
		mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		// 初始化10个不同颜色的画笔
		mPaint.setColor(Color.WHITE);
		mColors[0] = Color.BLUE;
		mColors[1] = Color.RED;
		mColors[2] = Color.GREEN;
		mColors[3] = Color.YELLOW;
		mColors[4] = Color.CYAN;
		mColors[5] = Color.MAGENTA;
		mColors[6] = Color.DKGRAY;
		mColors[7] = Color.WHITE;
		mColors[8] = Color.LTGRAY;
		mColors[9] = Color.GRAY;
		for (int i = 0; i < MAX_TOUCHPOINTS; i++) {
			mPaints[i] = new Paint();
			mPaints[i].setColor(mColors[i]);
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		int points = event.getPointerCount();
		if (points > MAX_TOUCHPOINTS) {
			points = MAX_TOUCHPOINTS;
		}
		// 先锁定画布，在进行处理
		Canvas c = mHolder.lockCanvas();
		if (c != null) {
			c.drawColor(Color.BLACK);
			final int action = event.getAction();
			if (action == MotionEvent.ACTION_UP) {

			} else {
				// 先在屏幕上画一个十字，然后画一个圆
				for (int i = 0; i < points; i++) {
					// 获取一个触点的坐标，然后开始绘制
					int id = event.getPointerId(i);
					int x = (int) event.getX(i);
					int y = (int) event.getY(i);
					drawCrosshairsAndText(x, y, mPaints[id], i, id, c);
				}
				for (int i = 0; i < points; i++) {
					int id = event.getPointerId(i);
					int x = (int) event.getX(i);
					int y = (int) event.getY(i);
					drawCircle(x, y, mPaints[id], c);
				}
			}
			// 画完后，unlock
			getHolder().unlockCanvasAndPost(c);
		}
		return true;
	}

	private void drawCrosshairsAndText(int x, int y, Paint paint, int ptr,
			int id, Canvas c) {
		c.drawLine(0, y, width, y, paint);
		c.drawLine(x, 0, x, height, paint);
		int textY = (int) ((15 + 20 * ptr) * scale);
		c.drawText("x" + ptr + "=" + x, 10 * scale, textY, mPaint);
		c.drawText("y" + ptr + "=" + y, 70 * scale, textY, mPaint);
		c.drawText("id" + ptr + "=" + id, width - 55 * scale, textY, mPaint);
	}

	private void drawCircle(int x, int y, Paint paint, Canvas c) {
		c.drawCircle(x, y, 40 * scale, paint);
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		this.width = width;  
        this.height = height;  
        if (width > height) {  
            this.scale = width / 480f;  
        } else {  
            this.scale = height / 480f;  
        }  
        mPaint.setTextSize(14 * scale);  
        Canvas c = getHolder().lockCanvas();  
        if (c != null) {  
            // 背景黑色  
            c.drawColor(Color.BLACK);  
            float tWidth = mPaint.measureText(START_TEXT);  
            c.drawText(START_TEXT, width / 2 - tWidth / 2, height / 2,  
            		mPaint);  
            getHolder().unlockCanvasAndPost(c);
        }
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {

	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {

	}
}
